//
//  ACNetWorkTool.h
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/4/12.
//

#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>


#pragma mark - 网络相关的宏

#define TIMEOUT_INTERVAL 10.0 //网络请求超时时间
#define NETReturnErrorCode 89999 //服务器报错的
#define NETWorkErrorCode 90000 //手机显示无网络
#define WebWorkErrorString @"网络开小差啦～"
#define NETWorkErrorString @"网络开小差啦～"


//统一公共错误识别
#define NetErrorWithPublicDomain @"Unifiedcommonerroridentification"
//数据返回格式格式不正确
#define NetErrorWithIncorrectformatDomain @"Incorrectformat"
#define NetErrorWithIncorrectformatInfo @"您要的数据暂时没有"


#define HOSTURL @"http://81.70.168.33"
#define PAGE_NUM 20 //分页请求的页数

NS_ASSUME_NONNULL_BEGIN

typedef void(^RequestHandler)(int code, NSString * _Nullable message);

@protocol ACNetWorkToolRequestDelegate <NSObject>

@optional

- (void)startRequestWithTask:(NSURLSessionDataTask* __unused)task paramsM:(NSMutableDictionary *)paramsM url:(NSString *)url fileName:(NSString *)fileName;
- (void)requestSuccessWithTask:(NSURLSessionDataTask* __unused)task responseObject:(id)responseObject paramsM:(NSMutableDictionary *)paramsM url:(NSString *)url fileName:(NSString *)fileName;
- (void)requestFailedWithTask:(NSURLSessionDataTask* __unused)task responseObject:(id)responseObject error:(NSError *)error params:(NSDictionary *)parmas url:(NSString *)url fileName:(NSString *)fileName;

@end

@interface ACNetWorkTool : AFHTTPSessionManager

@property (nonatomic,assign) AFNetworkReachabilityStatus status;

@property (nonatomic,strong) NSMutableArray *dataTaskArray;

@property (nonatomic, weak) id<ACNetWorkToolRequestDelegate> requestDelegate;

@property (nonatomic, strong) NSMutableDictionary *retryCountDic;

//@property(nonatomic, strong) NSRecursiveLock *retryLock;
@property(nonatomic, strong) dispatch_semaphore_t sem;
@property(nonatomic, strong) dispatch_queue_t queue;

+(instancetype)shareRequestManager;
/**
 get请求
 
 @param url 请求的API地址
 @param params 参数
 @param success 成功回调Block
 @param failure 失败回调Block
 */

/// get请求
- (NSURLSessionDataTask *)get:(NSString *)url params:(id)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

/**
 post请求
 
 @param url 请求的API地址
 @param params 参数
 @param success 成功回调Block
 @param failure 失败回调Block
 */

/// post请求
- (NSURLSessionDataTask *)post:(NSString *)url params:(id)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

/**
 del请求
 
 @param url 请求的API地址
 @param params 参数
 @param success 成功回调Block
 @param failure 失败回调Block
 */

/// del请求
- (NSURLSessionDataTask *)del:(NSString *)url params:(id)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;
/**
 put请求
 
 @param url 请求的API地址
 @param params 参数
 @param success 成功回调Block
 @param failure 失败回调Block
 */

/// 支持表单类型。contentType form / json / form-data
- (NSURLSessionDataTask *)post:(NSString *)url contentType:( NSString * _Nullable)contentType params:(id _Nullable)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

/// put请求
- (NSURLSessionDataTask *)put:(NSString *)url params:(id)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

/// 监听网络状态改变
+ (void)getNetworkStates;

/// 错误码判断拦截处理
- (BOOL)changeNetErrorCode:(NSString *)code andMsg:(NSString *)msg;

/// 添加默认请求头
- (NSMutableDictionary *)addDefaultHeader;

/// 上传文件
-(void)uploadFile:(NSString *)url filePath:(NSString *)filePath progress:(void (^)(NSProgress * _Nonnull uploadProgress))progress success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

@end

NS_ASSUME_NONNULL_END
